<?php
/**
 * RoomCategoryController
 *
 * PHP Version 5.4
 *
 * @author liuxu <liuxuphp@icesmart.cn>
 */

namespace Api\Controller;

use Admin\Service\RoomService;
use Model\LiveCateogyModel;
use Model\UserModel;
use Think\Controller;
use Model\RoomModel;

class RoomCategoryController extends BaseController
{
    /**
     * 直播间分类列表
     * @http-method GET
     */
    public function find()
    {
        $url = '/Api/'.CONTROLLER_NAME.'/'.ACTION_NAME;
        $validate = $this->validateInterface($url);
        if(!$validate){
            $result['code'] = 404;
            $result['msg'] = '此接口已被禁用';

            $this->ajaxReturn($result, 'JSON');
        }
        $filter['categoryId'] = 0;
        $filter['status'] = 1;

        $categoryModel = new LiveCateogyModel();
        $categoryList = $categoryModel->findByFilter($filter);
        foreach($categoryList as $k => $v){
            $categoryList[$k]['image'] = $_SERVER["SERVER_NAME"].'/'.$v['image'];
            $categoryList[$k]['nav'] = $categoryModel->where('categoryId = '.$v['id'])->select();
            foreach($categoryList[$k]['nav'] as $i => $j){
                $categoryList[$k]['nav'][$i]['image'] = $_SERVER["SERVER_NAME"].'/'.$j['image'];
            }
        }

        $result = [];
        if($categoryList){
            $result['code'] = 200;
            $result['msg'] = $categoryList;
        }else{
            $result['code'] = 404;
            $result['msg'] = '数据不存在';
        }

        $this->ajaxReturn($result, 'JSON');
    }


    /**
     * 获取分类下的直播间
     * @http-method GET
     */
    public function getRooms()
    {
        $url = '/Api/'.CONTROLLER_NAME.'/'.ACTION_NAME;
        $validate = $this->validateInterface($url);
        if(!$validate){
            $result['code'] = 404;
            $result['msg'] = '此接口已被禁用';

            $this->ajaxReturn($result, 'JSON');
        }
        $result = [];
        $categoryId = I('get.id');
        if(empty($categoryId)){
            $result['code'] = 404;
            $result['msg'] = '参数异常';

            $this->ajaxReturn($result, 'JSON');
        }

        $roomModel = new RoomModel();
        $roomList = $roomModel->where('categoryId = '.$categoryId.' AND status = 1')->select();

        if($roomList){
            $categoryModel = new LiveCateogyModel();
            $userModel = new UserModel();
            $roomService = new RoomService();
            $roomNums = $roomService->getRoomNums();
            foreach($roomList as $k => $v){
                $categoryInfo = $categoryModel->field('name')->get($categoryId);
                $roomList[$k]['categoryName'] = $categoryInfo['name'];

                $userInfo = $userModel->get($v['userId']);
                if($userInfo['from'] == 0){
                    $roomList[$k]['picture'] = $_SERVER["SERVER_NAME"].$userInfo['picture'];
                }else{
                    $roomList[$k]['picture'] = $userInfo['picture'];
                }
                $roomList[$k]['username'] = $userInfo['nickname'];

                //获取直播间在线人数
                $roomList[$k]['nums'] = 0;
                foreach($roomNums as $roomNum){
                    if($v['id'] == $roomNum['roomId']){
                        $roomList[$k]['nums'] = $roomNum['roomNums'];
                    }
                }
            }

            $roomList[$k]['image'] = $_SERVER["SERVER_NAME"].'/'.$roomList[$k]['image'];

            $result['code'] = 200;
            $result['msg'] = $roomList;
        }else{
            $result['code'] = 404;
            $result['msg'] = '数据不存在';
        }

        $this->ajaxReturn($result, 'JSON');
    }
} 